package com.kanshu.ksgb.fastread.doudou.module.personal.presenter;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.PageRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.DiskLruCacheUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.module.login.bean.ChargeBean;
import com.kanshu.ksgb.fastread.doudou.module.login.bean.ConsumeBean;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.CommentBean;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.ConfigBean;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UserBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wcy on 2017\6\23 0023.
 */

public class PersonCenterModel {
    List<Disposable> mDisposables = new ArrayList<>();

    public void getUserInfo(String user_id, final INetCommCallback<UserBean> callback) {
        UserBean userBean = DiskLruCacheUtils.get(user_id, UserBean.class);
        callback.onResponse(userBean);
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            callback.onError(Constants.ErrCode.NetErr, "no net");
            return;
        }
        RetrofitHelper helper = RetrofitHelper.getInstance();
        helper.createService(PersonCenterService.class)
                .getUserInfo("0")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new BaseObserver<UserBean>() {
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                callback.onError(Constants.ErrCode.UnknowErr, errDesc);
            }

            @Override
            public void onResponse(BaseResult<UserBean> info, UserBean userBean, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(disposable);
                if (BaseResult.isNotNull(info)) {
                    callback.onResponse(info.result.data);
                }
            }
        });
    }

    public void cancel() {
        Utils.dispose(mDisposables);
    }

    /**
     * 获取充值记录
     * @param callback
     */
    public void getChargeInfos(PageRequestParams params, final INetCommCallback<BaseResult<List<ChargeBean>>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            callback.onError(Constants.ErrCode.NetErr, "no net");
            return;
        }
        RetrofitHelper helper = RetrofitHelper.getInstance();
        helper.createService(PersonCenterService.class)
                .getChargeInfos(params, "1")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new BaseObserver<List<ChargeBean>>() {

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                callback.onError(code, errDesc);
            }

            @Override
            public void onResponse(BaseResult<List<ChargeBean>> info, List<ChargeBean> userBean, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(disposable);
                if (BaseResult.isNotNull(info)) {
                    callback.onResponse(info);
                }
            }
        });
    }

    /**
     * 充值记录
     * @param params
     * @param callback
     */
    public void getConsumeInfos(PageRequestParams params, final INetCommCallback<BaseResult<List<ConsumeBean>>> callback) {
        RetrofitHelper helper = RetrofitHelper.getInstance();
        helper.createService(PersonCenterService.class)
                .getConsumeInfos(params, "1")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new BaseObserver<List<ConsumeBean>>() {

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                callback.onError(Constants.ErrCode.UnknowErr, errDesc);
            }

            @Override
            public void onResponse(BaseResult<List<ConsumeBean>> info, List<ConsumeBean> userBean, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(disposable);
                if (BaseResult.isNotNull(info)) {
                    callback.onResponse(info);
                }
            }
        });
    }

    /**
     * 获取评论信息
     * @param params
     * @param callback
     */
    public void getCommentInfos(PageRequestParams params, final INetCommCallback<BaseResult<List<CommentBean>>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            callback.onError(Constants.ErrCode.NetErr, "no net");
            return;
        }
        RetrofitHelper helper = RetrofitHelper.getInstance();
        helper.createService(PersonCenterService.class)
                .getCommentInfos(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new BaseObserver<List<CommentBean>>() {
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                callback.onError(Constants.ErrCode.UnknowErr, errDesc);
            }

            @Override
            public void onResponse(BaseResult<List<CommentBean>> info, List<CommentBean> userBean, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(disposable);
                if (BaseResult.isNotNull(info)) {
                    callback.onResponse(info);
                }
            }
        });
    }

    public void getConfig(final INetCommCallback<ConfigBean> callback) {
        RetrofitHelper helper = RetrofitHelper.getInstance();
        helper.createService(PersonCenterService.class)
                .getConfig()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new BaseObserver<ConfigBean>() {
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mDisposable.isDisposed()) {
                    return;
                }
                callback.onError(Constants.ErrCode.UnknowErr, errDesc);
            }

            @Override
            public void onResponse(BaseResult<ConfigBean> info, ConfigBean userBean, Disposable disposable) {
                if (mDisposable.isDisposed()) {
                    return;
                }
                mDisposables.add(disposable);
                if (BaseResult.isNotNull(info)) {
                    callback.onResponse(userBean);
                }
            }
        });
    }


}
